package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.ClaimScoringService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.ClaimScoringDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ClaimScoring.
 */
@RestController
@RequestMapping("/api")
public class ClaimScoringResource {

    private final Logger log = LoggerFactory.getLogger(ClaimScoringResource.class);

    private static final String ENTITY_NAME = "claimScoring";

    private final ClaimScoringService claimScoringService;

    public ClaimScoringResource(ClaimScoringService claimScoringService) {
        this.claimScoringService = claimScoringService;
    }

    /**
     * POST  /claim-scorings : Create a new claimScoring.
     *
     * @param claimScoringDTO the claimScoringDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new claimScoringDTO, or with status 400 (Bad Request) if the claimScoring has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/claim-scorings")
    @Timed
    public ResponseEntity<ClaimScoringDTO> createClaimScoring(@RequestBody ClaimScoringDTO claimScoringDTO) throws URISyntaxException {
        log.debug("REST request to save ClaimScoring : {}", claimScoringDTO);
        if (claimScoringDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new claimScoring cannot already have an ID")).body(null);
        }
        ClaimScoringDTO result = claimScoringService.save(claimScoringDTO);
        return ResponseEntity.created(new URI("/api/claim-scorings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /claim-scorings : Updates an existing claimScoring.
     *
     * @param claimScoringDTO the claimScoringDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated claimScoringDTO,
     * or with status 400 (Bad Request) if the claimScoringDTO is not valid,
     * or with status 500 (Internal Server Error) if the claimScoringDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/claim-scorings")
    @Timed
    public ResponseEntity<ClaimScoringDTO> updateClaimScoring(@RequestBody ClaimScoringDTO claimScoringDTO) throws URISyntaxException {
        log.debug("REST request to update ClaimScoring : {}", claimScoringDTO);
        if (claimScoringDTO.getId() == null) {
            return createClaimScoring(claimScoringDTO);
        }
        ClaimScoringDTO result = claimScoringService.save(claimScoringDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, claimScoringDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /claim-scorings : get all the claimScorings.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of claimScorings in body
     */
    @GetMapping("/claim-scorings")
    @Timed
    public ResponseEntity<List<ClaimScoringDTO>> getAllClaimScorings(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ClaimScorings");
        Page<ClaimScoringDTO> page = claimScoringService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/claim-scorings");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /claim-scorings/:id : get the "id" claimScoring.
     *
     * @param id the id of the claimScoringDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the claimScoringDTO, or with status 404 (Not Found)
     */
    @GetMapping("/claim-scorings/{id}")
    @Timed
    public ResponseEntity<ClaimScoringDTO> getClaimScoring(@PathVariable Long id) {
        log.debug("REST request to get ClaimScoring : {}", id);
        ClaimScoringDTO claimScoringDTO = claimScoringService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(claimScoringDTO));
    }

    /**
     * DELETE  /claim-scorings/:id : delete the "id" claimScoring.
     *
     * @param id the id of the claimScoringDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/claim-scorings/{id}")
    @Timed
    public ResponseEntity<Void> deleteClaimScoring(@PathVariable Long id) {
        log.debug("REST request to delete ClaimScoring : {}", id);
        claimScoringService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/claim-scorings?query=:query : search for the claimScoring corresponding
     * to the query.
     *
     * @param query the query of the claimScoring search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/claim-scorings")
    @Timed
    public ResponseEntity<List<ClaimScoringDTO>> searchClaimScorings(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of ClaimScorings for query {}", query);
        Page<ClaimScoringDTO> page = claimScoringService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/claim-scorings");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
